<template>
  <el-card shadow="hover">
    <el-form label-width="65px" status-icon v-model="domain" ref="vform">
      <el-form-item label="标题" prop="title" required :rules="[{ required: true, message: '请输入标题', trigger: 'blur' }]">
        <el-input v-model="domain.title"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword" required :rules="[{ required: true, message: '请输入关键词', trigger: 'blur' }]">
        <el-input v-model="domain.keyword"></el-input>
      </el-form-item>
      <el-form-item label="分类">
        <el-input v-model="domain.category"></el-input>
      </el-form-item>
      <el-form-item label="封面">
        <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="domain.pageFaceimageUrl" :src="domain.pageFace" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="domain.contentType">
          <el-radio label="1">富文本</el-radio>
          <el-radio label="2">markdown</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容" prop="content" required :rules="[{ required: true, message: '请输入内容', trigger: 'blur' }]" v-if="'1' == domain.contentType">
        <quill-editor v-model="domain.content"></quill-editor>
      </el-form-item>
      <el-form-item label="内容" prop="content" required :rules="[{ required: true, message: '请输入内容', trigger: 'blur' }]" v-if="'2' == domain.contentType">
        <mavon-editor v-model="domain.content" :ishljs = "true"/>
      </el-form-item>
      <el-form-item>
        <!-- <el-button>返回</el-button> -->
        <el-button type="primary" @click="save(2)">保存</el-button>
        <el-button type="success" @click="save(1)">发布</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { quillEditor } from "vue-quill-editor";

import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";

export default {
  data() {
    return {
      domain: {
        title: "",
        keyword: "",
        category: "",
        content: "",
        contentType: "1",
        status: 1,
        pageFace: ""
      }
    };
  },
  components: {
    quillEditor,
    mavonEditor
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    save(status) {
      this.domain.status = status;
      this.$refs["vform"].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
